﻿/*
 * Created by SharpDevelop.
 * User: Alexey
 * Date: 23.01.2012
 * Time: 17:04
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Collections.Generic;

using System.Windows.Forms;

namespace DrawTest
{
	/// <summary>
	/// Description of Missile.
	/// </summary>
	public class Missile : FlyingObject
	{
		public Missile()
		{
		}
		
		public bool IsCrashSimulation { get; set; }
		
		
		public void initMissile(string imagePath, int meanSpeed, MyPoint startPoint, MyPoint endPoint){
			initializeFlying(imagePath, meanSpeed);
			objectPath = findPath(startPoint, endPoint);
			IsCrashSimulation = false;
		}
		
		public void drawMissile(Graphics dc, int currentAngle, List <AirCraft> airCrafts){
			if (nextMove() && !IsCrashSimulation){
				findAirCraft(airCrafts);
			}
			drawObject(dc, currentAngle);
		}
		
		public void findAirCraft(List <AirCraft> airCrafts)
		{

			int smallestLength = 100500;
			
			AirCraft necessaryAirCraft = new AirCraft();
			MyPoint missilePosition = getObjectPosition();
			
			foreach (AirCraft airCraft in airCrafts)
			{
				MyPoint currentAirCraftPosition = airCraft.getObjectPosition();
				
				
				if (missilePosition == null){
					break;
				}
				
				int sideY = Math.Abs(currentAirCraftPosition.Y - missilePosition.Y);
				int sideX = Math.Abs(currentAirCraftPosition.X - missilePosition.X);
				
				
				int range = 50;
				
				if (sideX < range && sideY < range){
					
					double ab = Math.Sqrt(sideX*sideX + sideY*sideY);
					int length = (int)Math.Round(ab);

					if (smallestLength > length){
						

						smallestLength = length;
						necessaryAirCraft =airCraft;
					}
				}
			}
			
			if (smallestLength != 100500){
				objectPath = new List<MyPoint>();
				currentPointPosition=0;
				
				MyPoint currentAirCraftPosition = necessaryAirCraft.getObjectPosition();
				
				objectPath = findPath(missilePosition, currentAirCraftPosition);
				
				if (objectPath.Count <= 3){
					necessaryAirCraft.IsKilled = true;
					missileAttack(currentAirCraftPosition);
				}
			}
		}
		
		private void missileAttack(MyPoint appearPosition)
		{
			IsCrashSimulation = true;
			loadImage("resources//fly_crash.png");
			
			objectPath = new List<MyPoint>();
			currentPointPosition=0;
			
			for (int i=0;i<10;i++){
				objectPath.Add(appearPosition);
			}
		}
		
		
	}
}
